define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/rush/index',
                    add_url: 'order/rush/add',
                    edit_url: 'order/rush/edit',
                    del_url: 'order/rush/del',
                    multi_url: 'order/rush/multi',
                    table: 'rush',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                showExport:false,
                showToggle: false,
                showColumns: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),operate:false},
                        {field: 'member.nickname', title: __('Member.nickname'),operate:"LIKE"},
                        {field: 'rushsn', title: __('Rushsn'),operate:"LIKE"},
                        {field: 'goods.name', title: __('Goods.name'),operate:"LIKE"},
                        {field: 'price', title: __('Price'), operate:false},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        {field: 'delivery', title: __('Delivery'),operate:false, searchList: {"0":__('Delivery 0'),"1":__('Delivery 1')}, formatter: Table.api.formatter.normal},
                        {field: 'createtime', title: __('Createtime'), operate:false, addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'openid', title: __('Openid')},
                        // {field: 'rushsn', title: __('Rushsn')},
                        // {field: 'goodsid', title: __('Goodsid')},
                        // {field: 'price', title: __('Price'), operate:'BETWEEN'},
                        // {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        // {field: 'delivery', title: __('Delivery'), searchList: {"0":__('Delivery 0'),"1":__('Delivery 1')}, formatter: Table.api.formatter.normal},
                        // {field: 'paytime', title: __('Paytime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'member.id', title: __('Member.id')},
                        // {field: 'member.openid', title: __('Member.openid')},
                        // {field: 'member.avatar_image', title: __('Member.avatar_image'), formatter: Table.api.formatter.image},
                        // {field: 'member.username', title: __('Member.username')},
                        // {field: 'member.nickname', title: __('Member.nickname')},
                        // {field: 'member.mobile', title: __('Member.mobile')},
                        // {field: 'member.agentid', title: __('Member.agentid')},
                        // {field: 'member.identity', title: __('Member.identity')},
                        // {field: 'member.membersn', title: __('Member.membersn')},
                        // {field: 'member.p_id', title: __('Member.p_id')},
                        // {field: 'member.credit', title: __('Member.credit'), operate:'BETWEEN'},
                        // {field: 'member.creditcode', title: __('Member.creditcode')},
                        // {field: 'member.bindtime', title: __('Member.bindtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'member.periodtime', title: __('Member.periodtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'member.createtime', title: __('Member.createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'goods.id', title: __('Goods.id')},
                        // {field: 'goods.name', title: __('Goods.name')},
                        // {field: 'goods.mprice', title: __('Goods.mprice'), operate:'BETWEEN'},
                        // {field: 'goods.gprice', title: __('Goods.gprice'), operate:'BETWEEN'},
                        // {field: 'goods.pprice', title: __('Goods.pprice'), operate:'BETWEEN'},
                        // {field: 'goods.image', title: __('Goods.image'), formatter: Table.api.formatter.image},
                        // {field: 'goods.images', title: __('Goods.images'), formatter: Table.api.formatter.images},
                        // {field: 'goods.gtype_id', title: __('Goods.gtype_id')},
                        // {field: 'goods.pdata', title: __('Goods.pdata')},
                        // {field: 'goods.longitude', title: __('Goods.longitude')},
                        // {field: 'goods.latitude', title: __('Goods.latitude')},
                        // {field: 'goods.address', title: __('Goods.address')},
                        // {field: 'goods.num', title: __('Goods.num')},
                        // {field: 'goods.volume', title: __('Goods.volume')},
                        // {field: 'goods.weigh', title: __('Goods.weigh')},
                        // {field: 'goods.createtime', title: __('Goods.createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        {field: 'operate', title: __('Operate'), table: table,
                            //在原有按钮的基础上增加一个 增加多个在buttons中添加即可
                            buttons: [
                                {
                                    name: 'delivery',
                                    text: __('核销'),
                                    title: __('核销'),
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    icon: 'fa fa-folder-o',
                                    url: 'order/rush/delivery/delivery/1',
                                    confirm: '确认核销?',
                                    success: function (data, ret) {
                                        if(ret.code == 1){
                                            Layer.alert(ret.msg);
                                            window.location.reload();
                                            return false;
                                        }else{
                                            Layer.alert(ret.msg);
                                            return false;
                                        }
                                    },
                                },
                            ],
                            events: Table.api.events.operate,
                            formatter: function (value,row,index) {
                                var that = $.extend({},this);
                                var table = $(that.table).clone(true);
                                //row.status == 1 && row.delivery == 0 按钮显示
                                if(row.status !=1 || row.delivery !=0){
                                    $(table).data('operate-delivery',null);
                                }
                                that.table = table;
                                return Table.api.formatter.operate.call(that,value,row,index);
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            $('#c-delivery').change(function () {
                layer.confirm('谨慎操作?',function () {
                    var ids = Config.id;
                    var delivery = $('#c-delivery option:selected').val();
                    $.ajax({
                        url:'order/rush/delivery',
                        method:'GET',
                        data:{'ids':ids,'delivery':delivery},
                        success:function(res){
                            layer.alert('操作成功');
                        }
                    })
                })
            })
            
            
            
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});